x1 <- ggplot()+
  geom_smooth(data = final_ns %>% filter(days < 1 & days >= -50), aes(days, affpol, color = factor(pid), fill = factor(pid)), method = 'loess')+
  geom_smooth(data = final_ns %>% filter(days > -1 & days <= 50), aes(days, affpol, color = factor(pid), fill = factor(pid)), method = 'loess')+
  #  geom_smooth(data = final %>% filter(days < 1 & days >= -50 & pid == "Democrat"), aes(days, affpol), method = 'loess', color = '#56B4E9', fill = "#56B4E9")+
  # geom_smooth(data = final %>% filter(days > -1 & days <= 50 & pid == "Democrat"), aes(days, affpol), method = 'loess', color = '#56B4E9', fill = "#56B4E9")+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(1,4))+
  scale_x_continuous(breaks = seq(-50,50,10))+
  xlab('Days before/after 2020 Election')+
  ylab('Affective Polarization')+
  labs(fill = "Political Party", color = 'Political Party')+
  scale_color_manual(values=c("#cc6677", "#56B4E9"),
                     labels=c('Republican', 'Democrat'))+
  scale_fill_manual(values=c("#cc6677", "#56B4E9"),
                    labels=c('Republican', 'Democrat'))+
  theme(legend.position = 'bottom')

## Gov Races

gov_races_2020 <- c('MT', 'NC', 'ND', 'UT', 'VT', 'WA', 'WV', 'MO', 'IN', 'DE', 'NH')

gov_races_dem <- c('DE', 'NC', 'WA')

stan_gov_ns <- final_ns %>%
  filter(state %in% gov_races_2020) %>%
  mutate(gov_won = case_when(
    state %in% gov_races_dem & pid == "Democrat" ~ 1,        
    !(state %in% gov_races_dem) & pid == "Democrat" ~ 0,      
    state %in% gov_races_dem & pid == "Republican" ~ 0,       
    !(state %in% gov_races_dem) & pid == "Republican" ~ 1))

x2 <- ggplot()+
  geom_smooth(data = stan_gov_ns %>% filter(days < 1, days >= -50), aes(days, affpol,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess', span = 2)+
  geom_smooth(data = stan_gov_ns %>% filter(days > -1, days <= 50), aes(days, affpol,  color = factor(gov_won), fill = factor(gov_won)), method = 'loess', span = 2)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(1,4))+
  scale_x_continuous(breaks = seq(-50,50,10))+
  xlab('Days before/after 2020 Election')+
  ylab('Affective Polarization')+
  labs(fill = "Governor Candidate", color = 'Governor Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'bottom')


## Senate Races

senate_races_2020 <- c('AL', 'AK', 'AZ', 'AR', 'CO', 'DE', 'GA', 'ID', 'IL', 'IA', 
                       'KS', 'KY', 'LA', 'ME', 'MA', 'MI', 'MN', 'MS', 'MT', 'NE', 
                       'NH', 'NJ', 'NM', 'NC', 'OK', 'OR', 'RI', 'SC', 'SD', 'TN', 
                       'TX', 'VA', 'WV', 'WY')


senate_races_dems <- c('AZ', 'CO', 'DE', 'GA', 'IL', 'MA', 'MI', 'MN', 'NH', 'NJ', 
                       'NM', 'OR', 'RI', 'VA')


stan_senate_ns <- final_ns %>%
  filter(state %in% senate_races_2020) %>%
  mutate(senate_won = case_when(
    state %in% senate_races_dems & pid == "Democrat" ~ 1,        
    !(state %in% senate_races_dems) & pid == "Democrat" ~ 0,      
    state %in% senate_races_dems & pid == "Republican" ~ 0,       
    !(state %in% senate_races_dems) & pid == "Republican" ~ 1))


x3 <- ggplot()+
  geom_smooth(data = stan_senate_ns %>% filter(days < 1, days >= -50), aes(days, affpol,  color = factor(senate_won), fill = factor(senate_won)), method = 'loess', span = 2)+
  geom_smooth(data = stan_senate_ns %>% filter(days > -1, days <= 50), aes(days, affpol,  color = factor(senate_won), fill = factor(senate_won)), method = 'loess', span = 2)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(1,4))+
  scale_x_continuous(breaks = seq(-50,50,10))+
  xlab('Days before/after 2020 Election')+
  ylab('Affective Polarization')+
  labs(fill = "Senate Candidate", color = 'Senate Candidate')+
  scale_color_manual(values = c("#cc6677", "#1c9906"),
                     labels=c('Lost', 'Won'))+
  scale_fill_manual(values = c("#cc6677", "#1c9906"),
                    labels=c('Lost', 'Won'))+
  theme(legend.position = 'bottom')


final_figure_win_ns <- suppressMessages(ggpubr::ggarrange(x1,x2,x3, ncol = 1))
suppressMessages(print(final_figure_win_ns))
#ggplot2::ggsave(final_figure_win_ns, file = 'final_figure_win_nationscape.png', units = 'in', height = 10, width = 10)